//
//  SQEmergencyRescueViewController.m
//  energy
//
//  Created by 苏青 on 2021/9/30.
//

#import "SQEmergencyRescueViewController.h"

#import "SQLookPlanViewController.h"

#import "SQReleaseModel.h"

@interface SQEmergencyRescueViewController ()<BaseTableVCInterface>

@end

@implementation SQEmergencyRescueViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"应急救援";
    [self getReleaseData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getReleaseData) name:@"addRelease" object:nil];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getReleaseData];
    }];
    LC_WEAK_SELF
    self.cellTitleIndex = ^(NSString * str, int key) {
        SQReleaseModelData * dataModel = weakSelf.dataSouece[key];
        if ([str isEqualToString:@"measures"]) {
           
            SQLookPlanViewController * lookPlanVC = SQLookPlanViewController.new;
            lookPlanVC.erp_er = dataModel.er_id;
            lookPlanVC.er_grade_id = dataModel.er_grade_id;
            [weakSelf pushVC:lookPlanVC];
        }else if ([str isEqualToString:@"phone"]){
           
            NSString *strUrl = [dataModel.er_responsible_text stringByReplacingOccurrencesOfString:@"、" withString:@"-"];
            NSArray *telAndPeoplelArr = [strUrl componentsSeparatedByString:@","];
            
            NSArray *a1 = [dataModel.er_responsible_text componentsSeparatedByString:@","];
              NSMutableArray *all = @[].mutableCopy;
              for (NSString *str in a1) {
                  NSArray *a2 = [str componentsSeparatedByString:@"、"];
                  [all addObject:a2.lastObject];
              }
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"报警电话"
                                                                               message:nil
                                                                        preferredStyle:UIAlertControllerStyleActionSheet];
            for (int i = 0; i < telAndPeoplelArr.count; i ++) {
//                    SQKeyAlarmModelData * dataModel = weakSelf.keyAlarmModel.data[i];
                UIAlertAction* telAction = [UIAlertAction actionWithTitle:telAndPeoplelArr[i] style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {

                    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",all[i]]];

                    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {

                    }];
                    

                }];
                [alert addAction:telAction];
            }
            
            
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      NSLog(@"action = %@", action);
                                                                  }];
            [alert addAction:cancelAction];
//
            [weakSelf presentViewController:alert animated:YES completion:nil];
            
        }
    };
}

-(void)getReleaseData{
    [SQReleaseModel getDataWithParams:@{@"er_enterprise":MEnterpriseId,@"type":@"0"} WithCallBack:^(SQReleaseModel * data) {
        [self detailModelwithArr:data.data withCallBack:nil];
        [self reloadTableView];
        
    }];
}

-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    return self.dataSouece[indexPath.row];
}



-(NSInteger)rowsCount:(NSInteger)section{
    return self.dataSouece.count;
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQEmergencyRescueTableViewCell"]];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
